﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using Tb.App.API.Applications.Command;
using Tb.App.API.Applications.DTOs;
using Tb.App.Domain.Rabbit.Basic;
using Tb.App.Domain.Rabbit.Cultivation;
using Tb.App.Domain.Rabbit.DeepProcessing;
using Tb.App.Domain.Rabbit.Slaughter;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;
using static NPOI.HSSF.Util.HSSFColor;

namespace Tb.App.API.Applications.CommandHandler
{
    /// <summary>
    /// 查询溯源关联所有信息
    /// </summary>
    public class ExportTraceabilityCommandHandler : IRequestHandler<ExportTraceabilityCommand, ApiResult<List<TraceabilityDTO>>>
    {
        private readonly IBaseRepository<BasicModel> basicRep;
        private readonly IBaseRepository<CultivationModel> culRep;
        private readonly IBaseRepository<DeepProcessingModel> deepRep;
        private readonly IBaseRepository<SlaughterModel> slaughterRep;

        public ExportTraceabilityCommandHandler(IBaseRepository<BasicModel> basicRep,
            IBaseRepository<CultivationModel> culRep,
            IBaseRepository<DeepProcessingModel> deepRep,
            IBaseRepository<SlaughterModel> slaughterRep)
        {
            this.basicRep = basicRep;
            this.culRep = culRep;
            this.deepRep = deepRep;
            this.slaughterRep = slaughterRep;
        }
        public  async Task<ApiResult<List<TraceabilityDTO>>> Handle(ExportTraceabilityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<TraceabilityDTO>> result=new ApiResult<List<TraceabilityDTO>>();
            var list = from a in  basicRep.GetAll().Where(a => !a.IsDel&&a.Batch==request.Batch)
                        join b in culRep.GetAll().Where(b => !b.IsDel)
                        on a.Batch equals b.Batch
                        join c in deepRep.GetAll().Where(c => !c.IsDel)
                        on a.Batch equals c.Batch
                        join d in slaughterRep.GetAll().Where( d=> !d.IsDel)
                        on a.Batch equals d.Batch
                        //where a.Batch == request.Batch
                        select new TraceabilityDTO
                        {
                            Basic = new BasicModelDTO
                            {
                                Id = a.Id,
                                Batch = a.Batch,
                                Sid = a.Sid,
                                Expiration = a.Expiration,
                                License = a.License,
                                Company = a.Company
                            },
                            Cultivation = new CultivationModelDTO
                            {
                                Variety       =b.Variety,
                                Source        =b.Source,
                                RearingDays   =b.RearingDays,
                                Brand         =b.Brand,
                                RearingLeader =b.RearingLeader,
                                OutDate       =b.OutDate,
                                Batch         =a.Batch,
                            },
                            DeepProcessing=new DeepProcessingModelDTO
                            {
                                Team=c.Team,
                                Batch=a.Batch,
                                Leader=c.Leader
                            },
                            Slaughter=new SlaughterModelDTO
                            {
                                Batch=a.Batch,
                                Weight=d.Weight,
                            }
                        };

            result.Code = ApiEnum.查询成功;
            result.Message = "查询成功";
            result.Data =await list.ToListAsync();
            return await Task.FromResult(result);
            
        }
    }
}
